const express = require('express');
const mysql = require('mysql');
const cors = require('cors');
const server = express();
const io  = require('./app1')
server.listen(4000);
server.use(cors({
    origin:['http://127.0.0.1:8080', 'http://localhost:8080'],
    credentials: true
}))
//引入插件，作用为解析post请求
const bodyParser = require('body-parser');
//配置body-parser插件
var urlencodedParser = bodyParser.urlencoded({extended:false})
var pool = mysql.createPool({
   host:'127.0.0.1',
   user:'root',
   password:'',
   database:'zhihu',
   connectionLimit: 15
})

server.get('/index',(req,res)=>{
   res.send('ok')
})
server.get('/articles',(req,res)=>{
   var type = req.query.type;
   var page = req.query.page;
   var pagesize = 5;
   var offset = (parseInt(page) - 1) * pagesize;
   //console.log(page);
   var pagecount = 0;
   var count = `SELECT COUNT(zhihu_articles.id) AS count
   FROM zhihu_articles,zhihu_category
   WHERE category_id = zhihu_category.id
   AND category_info = ?`;
   pool.query(count,[type],(error,results)=>{
       if(error) throw error;
       pagecount = Math.ceil(results[0].count / pagesize)
   })
   var sql = `SELECT zhihu_articles.id,subject,description,image
   FROM zhihu_articles,zhihu_category
   WHERE category_id = zhihu_category.id
   AND category_info=?
   LIMIT ${offset},${pagesize}`;
   pool.query(sql,[type],(error,results)=>{
       if(error) throw error;
       res.send({ code: '1', message: '查询成功', articles: results,pagecount: pagecount})
   });
});
server.get('/article',(req,res)=>{
   var id = req.query.id;
   var sql = `SELECT * FROM zhihu_articles WHERE id=? `
   pool.query(sql,[id],(error,result)=>{
       if(error) throw error;
       res.send({code:1,message:'查询成功',article:result[0]})
       //console.log(result)
   })
});

server.post('/register',urlencodedParser,(req,res)=>{
    var username = (req.body.uname);
    var password = (req.body.password);
    var sql = 'SELECT COUNT(id) AS count FROM zhihu_users WHERE username=?';
    pool.query(sql,[username],(error,result)=>{
        if (error) throw error;
        if(!result[0].count){
            //没有重名，可以开始注册
            var sql = 'INSERT zhihu_users(username,password) VALUES(?,MD5(?))';
            pool.query(sql,[username,password],(error,results)=>{
              if(error) throw error;
              res.send({message:'注册成功',code:'1'})
            })
        }else{
            //有重名，返回错误提示
            res.send({message:'用户名已经存在',code:'0'})
        }
    })
});


server.post('/login',urlencodedParser,(req,res)=>{
    var username = req.body.uname;
    var password = req.body.password;
    //console.log(username);
    //console.log(password);
    var sql = 'SELECT * FROM zhihu_users WHERE username=? AND password=MD5(?)';
    pool.query(sql,[username,password],(error,results)=>{
        if(error) throw error;
        if(results.length == 0){
             res.send({message:'登录错误',code:0})
        }else{
            res.send({message:'登录成功',code:1,username:results[0].username})
        }
    })
})

